//
//  AttributedText.swift
//  d11
//
//  Created by peale on 2018/7/26.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import Foundation
import UIKit
extension UIView{
    /**
     *匹配 改变部分颜色
     *string: 源字符串
     *subString: 子串
     *color: 子串的颜色
     **/
    func attributedTextColorWithString(string : NSString,subString : NSString,color : UIColor ) -> NSMutableAttributedString {
        let attributedText  = NSMutableAttributedString.init(string: string as String)
        if subString == "" {
            return attributedText
        }
        let stringTextRange : NSRange  = string.range(of: subString as String)
        attributedText.setAttributes([
            NSAttributedString.Key.foregroundColor: color
            ], range: stringTextRange)
        return attributedText
    }
    
    /**
     *匹配 添加改变部分颜色
     *atributedString 需要添加的富文本字符串
     *string: 源字符串
     *subString: 子串
     *color: 子串的颜色
     **/
    func attributedTextColorAddWithAttributedString(attributedString : NSMutableAttributedString,string : NSString,subString : NSString,color : UIColor ) -> NSMutableAttributedString {
        if subString == "" {
            return attributedString
        }
        let stringTextRange : NSRange  = string.range(of: subString as String)
        attributedString.addAttributes([
            NSAttributedString.Key.foregroundColor: color
            ], range: stringTextRange)
        return attributedString
    }
    /**
     *匹配 改变部分字体
     *string: 源字符串
     *subString: 子串
     *font: 子串的字体
     **/
    func attributedTextFontWithString(string : NSString,subString : NSString,font : UIFont ) -> NSMutableAttributedString {
        let attributedText  = NSMutableAttributedString.init(string: string as String)
        if subString == "" {
            return attributedText
        }
        let stringTextRange : NSRange  = string.range(of: subString as String)
        attributedText.setAttributes([
            NSAttributedString.Key.font: font
            ], range: stringTextRange)
        return attributedText
    }
    
    /**
     *匹配 添加改变部分字体
     *atributedString 需要添加的富文本字符串
     *string: 源字符串
     *subString: 子串
     *font: 子串的字体
     **/
    func attributedTextFontAddWithAttributedString(attributedString : NSMutableAttributedString,string : NSString,subString : NSString,font : UIFont ) -> NSMutableAttributedString {
        if subString == "" {
            return attributedString
        }
        let stringTextRange : NSRange  = string.range(of: subString as String)
        attributedString.addAttributes([
            NSAttributedString.Key.font: font
            ], range: stringTextRange)
        return attributedString
    }
    
    /**
     *匹配多个 改变部分颜色
     *string: 源字符串
     *subString: 子串
     *color: 子串的颜色
     **/
    func attributedTextMultiColorWithString(string : NSString,subString : NSString,color : UIColor ) -> NSMutableAttributedString {
        let attributedText  = NSMutableAttributedString.init(string: string as String)
        do{
            let regularexpression :NSRegularExpression = try NSRegularExpression.init(pattern: subString as String, options: .caseInsensitive)
         let match = regularexpression.matches(in: string as String, options: NSRegularExpression.MatchingOptions.anchored, range:NSRange.init(location: 0, length: string.length))
            if match.count != 0 {
                for matc in match{
                    let range = matc.range
                    if range.length > 0 {
                        attributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
                    }
                }
            }
        }catch{}
        return attributedText
    }
}
